package com.gray.wow.web.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gray.wow.biz.ArticleBiz;
import com.gray.wow.biz.ContentBiz;
import com.gray.wow.biz.FamilyBiz;
import com.gray.wow.biz.IndexBiz;
import com.gray.wow.biz.NoticeBiz;
import com.gray.wow.biz.PhotoBiz;
import com.gray.wow.common.DateUtil;
import com.gray.wow.common.Status;
import com.gray.wow.domain.PageModel;
import com.gray.wow.domain.Tag;
import com.gray.wow.domain.User;

@Controller
public class IndexController {
	@Resource
	private ContentBiz contentBiz;
	@Resource
	private ArticleBiz articleBiz;
	@Resource
	private PhotoBiz photoBiz;
	@Resource
	private NoticeBiz noticeBiz;
	@Resource
	private FamilyBiz familyBiz;
	@Resource
	private IndexBiz indexBiz;

	private String openTime = null;
	private String maxTime = null;

	public static final int ARTICLE_PAGESIZE = 8;

	@RequestMapping(value = { "/test" })
	public String indexPage(HttpServletRequest request, Model model) {
		Map<String, Object> map = indexBiz.getIndexPage();
		model.addAllAttributes(map);
		return "index/indexTest";
	}

	@RequestMapping("/seo/manage")
	public String seo(HttpServletRequest request) {
		Map<String, String> map= indexBiz.getMeta();
		request.setAttribute("map", map);
		return "admin/seo/seo";
	}
	
	@RequestMapping(value = {"/", "/index"})
	public String welcome(HttpServletRequest request, Model model) {
		List<Tag> tcList = contentBiz.getTagWidthContent();
		model.addAttribute("tcList", tcList);
		PageModel pm5 = contentBiz.getContentByOpenTime(
				DateUtil.getDateStr("yyyy-MM-dd", new Date()), maxTime, 30);// 今日新开
		model.addAttribute("pm5", pm5);
		PageModel pm6 = contentBiz.getContentByOpenTime(openTime,
				DateUtil.getDateStr("yyyy-MM-dd", new Date()), 30);// 三星推荐
		model.addAttribute("pm6", pm6);
		PageModel familyPm = familyBiz.getFamily(10, 1,
				"place desc,clicks desc", Status.PASS.getValue());// 家族
		model.addAttribute("familyPm", familyPm);

		return "index/index";
	}

	@RequestMapping(value = "/index/create")
	public @ResponseBody
	String createIndexPage(HttpServletRequest request) {
		User admin = (User) request.getSession().getAttribute("admin");
		if (admin != null) {
			String directory = "/WEB-INF/views/template";
			String realPath = request.getSession().getServletContext().getRealPath(directory);
			indexBiz.createIndexPage(realPath);
			return new String("success");
		}
		return new String("failed");
	}

	@RequestMapping(value = "/url")
	public String url(String u) {
		return "go";
	}
	
	@RequestMapping(value = "/go")
	public String go(String u) {
		return "yaoabc";
	}

}
